package Simulation;

import Config.Config;
import People.People;
import People.Status;
import Simulation.Manager;

/**
 * Moves People in the map
 * 
 * @author Amir BEN SLIMANE
 */
public class Move extends Manager {
	private Map map;
	private People[][] mapArray;

	/**
	 * Builder's Move
	 * 
	 * @param map
	 */
	public Move(Map map) {
		this.setMap(map);
		mapArray = this.getMap().getMapSimulation();
	}

	/**
	 * Random move all People
	 */
	public void moveEverybodyRandom() {
		for (int x = 0; x < Config.MAP_HEIGTH; x++) {
			for (int y = 0; y < Config.MAP_LENGTH; y++) {
				if (this.map.getMapSimulation()[x][y].getStatus() != Status.Dead) {
					int rand = (int) (Math.random() * 8);
					move(x, y, rand);
				}
			}
		}
	}

	private void move(int x, int y, int rand) {
		switch (rand) {
		case 0: // North
			switchPeople(mapArray[x][y], mapArray[x][Manager.mod(y - 1, Config.MAP_HEIGTH)]);
			break;
		case 1: // South
			switchPeople(mapArray[x][y], mapArray[x][Manager.mod(y + 1, Config.MAP_HEIGTH)]);
			break;
		case 2: // East
			switchPeople(mapArray[x][y], mapArray[Manager.mod(x + 1, Config.MAP_LENGTH)][y]);
			break;
		case 3: // West
			switchPeople(mapArray[x][y], mapArray[Manager.mod(x - 1, Config.MAP_LENGTH)][y]);
			break;
		case 4: // North East
			switchPeople(mapArray[x][y], mapArray[Manager.mod(x + 1, Config.MAP_LENGTH)][Manager.mod(y - 1, Config.MAP_HEIGTH)]);
			break;
		case 5: // North West
			switchPeople(mapArray[x][y], mapArray[Manager.mod(x - 1, Config.MAP_LENGTH)][Manager.mod(y - 1, Config.MAP_HEIGTH)]);
			break;
		case 6: // South East
			switchPeople(mapArray[x][y], mapArray[Manager.mod(x + 1, Config.MAP_LENGTH)][Manager.mod(y + 1, Config.MAP_HEIGTH)]);
			break;
		case 7: // South West
			switchPeople(mapArray[x][y], mapArray[Manager.mod(x - 1, Config.MAP_LENGTH)][Manager.mod(y + 1, Config.MAP_HEIGTH)]);
			break;
		default:
			break;
		}
	}


	/**
	 * @return the map
	 */
	public Map getMap() {
		return map;
	}

	/**
	 * @param map
	 *            the map to set
	 */
	public void setMap(Map map) {
		this.map = map;
	}
}
